$(document).ready(
		function() {
			// 供应商下拉框的数据绑定
			var supplierVue = new Vue({
				el : '#supplierDiv',
				data : {
					options : null
				},
				beforeCreate : function() {
					var _self = this;
					$.get(SERVER_PRE_URL + "user/getAllSupplierAjax",
							function(data) {
								_self.options = data;
							});
				}
			});
			// 选择进货原料的列表的数据绑定
			var materialListVue = new Vue({
				el : '#materialListData',
				data : {
					items : null
				},
				methods : {
					onclick : function(material, num) {
						openDeleteMaterialConfirm(material, num);
					}
				}
			});


			$("#inputTotalPrice").val(0);

			// 选择进货的原料list
			var materialList = new Array();

			// 选择进货商品
			$("#chooseFoodButton").on("click", function() {
				openChooseMaterialBox();
			});

			// 提交采购申请按钮
			$("#submitButton").on("click", function() {
				submitPurchaseOrder();
			});

			function submitPurchaseOrder() {
				var data = {
					"supplierId" : $("#inputSupplier").val(),
					"purchaseTime" : $("#inputPurchaseTime").val(),
					"totalPrice" : $("#inputTotalPrice").val(),
					"createById" : $("#inputCreateById").val(),
					"remark" : $("#inputRemark").val(),
					"materialList" : JSON.stringify(materialList)
				};

				baseUtil.httpSend("purchaseOrder/createNewPurchaseOrderAjax",
						data, showSubmitResult);
			}

			function showSubmitResult(result) {
				if (result == 1) {
					layer.alert('创建成功', {
						skin : 'layui-layer-molv' // 样式类名
						,
						closeBtn : 0
					}, function() {
						$("#contentIframe", parent.document.body).attr("src",
								JSP_PRE_URL + "editPurchaseOrder");
					});
				} else {
					layer.msg("创建失败！");
				}

			}

			// 更新应付账款的总价
			function updateMaterialListTotalPrice() {
				var totalPrice = 0;
				for ( var i in materialList) {
					totalPrice = totalPrice + materialList[i].totalPrice;
				}
				$("#inputTotalPrice").val(totalPrice);
			}

			function openDeleteMaterialConfirm(material, num) {
				layer.confirm('确认删除:' + material.materialName, {
					btn : [ '确认', '取消' ]
				// 按钮
				}, function() {
					layer.msg('删除成功!');
					materialList.splice(num, 1);
					Vue.set(materialListVue, "items", materialList);
					updateMaterialListTotalPrice();
				}, function() {

				});
			}

			function openChooseMaterialBox() {
				chooseMaterialBox = layer.open({
					type : 2,
					offset : '10%',
					maxmin : true,
					title : '选择原料',
					area : [ '85%', '85%' ],
					btn : [ '确认', '取消' ] // 只是为了演示
					,
					yes : function() {
						var childBox = layer.getChildFrame('body',
								chooseMaterialBox);
						var m = childBox.find("#materialList").val();

						// 更新选择数据
						if (m != "") {
							m = eval('(' + m + ')');
							for ( var i in m) {
								materialList.push(m[i]);
							}

							Vue.set(materialListVue, "items", materialList);
							updateMaterialListTotalPrice();
						}

						layer.close(chooseMaterialBox);

					},
					btn2 : function() {
						layer.close();
					},
					shadeClose : true, // 点击遮罩关闭
					content : JSP_PRE_URL + 'chooseMaterial'
				});

			}


		});